<?php
/**
 * THis Controller is write by leo
 * @author Leo
 *
 */
class MenuController extends Zend_Controller_Action
{
	
	/**
	 * Add by Leo
	 * This function is assign page to different departments, rare used
	 */
	public function menuLinkAction(){
		$menu = new Model_DbTable_Menulink();
		date_default_timezone_set('Australia/Melbourne');
		$datechange = date('Y/m/d', time());


		if($_POST){
			if($_POST['link_title']!=null&&$_POST['link_Description']!=null&&$_POST['link']!=null&&$_POST['user']!=null){
				$menu->addMenuLink($_POST['link_title'], $_POST['link_Description'], $_POST['link'], $_POST['user'], $datechange);
			}
		}
		$list = $menu->listAll();
		$this->view->list = $list;
		
	}		
	
	public function staffRateAction(){
		/*
		set_time_limit(0);
		
		$shopArr = unserialize(ARR_SHOP_ACTIVE);
		$location = new Model_Products_Location();
		$arrPSales = array();
		$noSaleList = array();
		$diff = Model_DatetimeHelper::dateDiff('2015-03-30','2015-04-26');
		$arrDate = Model_DatetimeHelper::createDateArray('2015-03-30','2015-04-26');
		if($_POST){
			$file = $_FILES['file']['tmp_name'];
			if(empty ($file)){
				$this->view->errorFlag2 = true;
					
				$this->view->errorMessage = "Please import the file";
				$this->renderScript('/products/importCsv.phtml');
				return false;
			}
				
			$arrResult = array();
		
			$fl = fopen($file,"r");
		
			while(($lineData = fgetcsv($fl,0)) != false){
					
				$arrResult[$lineData[0]] = array($lineData[1],$lineData[2],$lineData[3],$lineData[4]);
			}
		
			fclose($fl);
		
				$expfolder =  getcwd()."/tmp/shop/";
				$file2 = "IP5.txt";
				$f2 = fopen($expfolder.$file2,"w");
				
				foreach ($arrResult as $k => $v){
					if(!empty($k)){
					$locat = $location->getLocationsByProduct($k);
					$qty = Model_System_Downloadhelper::downloadUrl('http://'.SERVER_IP_COMM.'/products/get-current-stock/barcode/'.$k);

					fputs($f2,$k."\t".$v[0]."\t".$v[1]."\t".$v[2]."\t".$v[3]."\t".$qty."\t".$locat[0]['code_location']."\n");
					}
				}
				
				fclose($f2);

				echo "<a href=\"/tmp/shop/{$file2}\" target=\"_blank\" >Right Click  Here And Save AS </a>";
			
			
		}
		*/
		
		$stfDetail = new Model_DbTable_Roster_Stafflogindetail();
		$stfInfo = new Model_DbTable_Roster_Staffinfo();
		$pack = unserialize(PAYROLL_TYPE);
		
		$activeList = $stfDetail->listAllActiveStaff();
		$stfList = array();
		foreach ($activeList as $k => $v){
			$info = $stfInfo->getStaffinfo($v['id']);
			if($v['a'] == 1 && ($info['id_package'] == 5 || $info['id_package'] == 2)){
				$stfList[$v['id']]['name'] = $info['first_name']." ".$info['last_name'];
				$stfList[$v['id']]['nick'] = $v['ni'];
				$stfList[$v['id']]['package'] = $info['id_package'];
				$stfList[$v['id']]['type'] = Model_EncryptHelper::getSslPassword($info['visa_type']);
				$stfList[$v['id']]['rate'] = $info['new_rate'];
				$stfList[$v['id']]['register'] = $info['register_date'];
				
			}
		}
		$file2 = "Staff.txt";
		$expfolder =  getcwd()."/tmp/";
		$f2 = fopen($expfolder.$file2,"w");
		foreach ($stfList as $k => $v){

				fputs($f2,$k."\t".$v['name']."\t".$v['nick']."\t".$pack[$v['package']][1]."\t".$v['type']."\t".$v['rate']."\t".$v['register']."\r\n");

		}
		fclose($f2);
		
	}
	
	
	public function problemRepairJobAction(){
		$rJob = new Model_DbTable_Repairjob();
		$rMove = new Model_DbTable_Pr_Prstockmovement();
		$pInfo = new Model_DbTable_Pr_Prproducts();
		$pList = array();
		//d($rList);
		$tmpjobList = $rJob->listAllFinishedJobsByDate('2015-04-01', '2015-07-31');
		$sort = array();
		foreach($tmpjobList as $job){
				
			if($job['shop_code'] == 'KCPC' || $job['shop_code'] == 'NLPC' || $job['shop_code'] == 'BHPC' || $job['repair_staff'] == 380 || $job['shop_code'] == 'HPIC'){
		
				$shop =  strtoupper(trim($job["shop_code"]));
		
				//get parts bottom price
				$partsRes = $rMove->getPartsSummaryByJobId2($job['id_job'],"price_q");

				$policyArray = $rJob->bonusRule($job["id_job"]); //get policy
				//d($partsRes);
		
				//WHo , what ,finish time , quote , invoices , who record
				$tmpArr = array();
		
				$tmpArr["id_job"] = $job["id_job"];
				$tmpArr["date_start"] =  $job["date_start"];
				$tmpArr["repair_staff"] = $job["repair_staff"];
		
		
		
				$tmpArr["shop_code"] = $shop;
				$tmpArr["mobile_brand"] = $job["mobile_brand"];
				$tmpArr["mobile_model"] = $job["mobile_model"];
				$tmpArr["quot_price"] = $job["quot_price"];
				$tmpArr["invoice_no"] = $job["invoice_no"];
				$tmpArr["invoice_amt"] = $job["invoice1_price"];
				$tmpArr["invoice_no2"] = $job["invoice_no2"];
				$tmpArr["invoice_amt2"] = $job["invoice2_price"];
				$tmpArr["staff_record"] = $job["staff_record"];
				$tmpArr["time_record"] = $job["time_record"];
				$tmpArr["part_sum"] = $partsRes;
		
		
		
				$amount = 0;
				$totalCost = 0;

				if($job["invoice_no"]== $job["invoice_no2"]){
					$amount = $job["invoice1_price"];
				}
				else{
					$amount = $job["invoice1_price"] + $job["invoice2_price"];
				}

				if($partsRes[2] == 0){
					$tmpArr["policy_custparts"] = 1;
				}else{
					if($policyArray[1]){
						$tmpArr["policy_custparts"] = $policyArray[1];
					}else{
						$tmpArr["policy_custparts"] = 0;
					}
				}
				
		
				$totalCost = $partsRes[1];
				$tmpArr['apos_charge'] = $amount;
				$tmpArr['parts_cost'] = $totalCost;
				$arrRes[]  = $tmpArr;
					
			}
		}
		$list = array();
		foreach ($arrRes as $k => $v){
				
			if($v['apos_charge'] < ($v['parts_cost']*1.1) && !empty($v['repair_staff'])){
				if($v['repair_staff'] != 380){
					$list[$v['shop_code']][] = $v;
				}else{
					$list['ALEX'][] = $v;
				}
			}
				
		}

		$this->view->list = $list;
	}
	
	
	
	public function checkLateAction(){
		$att = new Model_DbTable_Roster_Attnrecord();
		$ts = new Model_DbTable_Roster_Timesheet();
		$stfDetail = new Model_DbTable_Roster_Stafflogindetail();
		date_default_timezone_set('Australia/Melbourne');
		$shopList = unserialize(ARR_SHOP_ACTIVE);
		
		foreach ($shopList as $v){
			if($v == 'KCPC' || $v == 'NLPC'){
				$tsList = $ts->listConfirmedShiftByDateByShop($v, '2015-03-01', '2015-06-28');
				d($tsList);
			}
		}
		
		
	}
	/**
	 * Add by leo
	 * This function is for record all logs of IT job.
	 * Add and show history of log.
	 */
	public function logRecordAction(){
		$log = new Model_DbTable_Updatelog();
		date_default_timezone_set('Australia/Melbourne');
		$datechange = date("Y-m-d H:i");
		$dateToday = Model_DatetimeHelper::dateToday();
		
		if($_POST){
			if(!empty($_POST['receiver'])){
				$log->addLog($datechange, $_POST['staff'], $_POST['type'], $_POST['sub_type'], $_POST['detail'], $_POST['link'],$_POST['status'],$dateToday,$_POST['receiver']);
			}else{
				$log->addLog($datechange, $_POST['staff'], $_POST['type'], $_POST['sub_type'], $_POST['detail'], $_POST['link'],$_POST['status'],$dateToday);
			}
		}
		
		$list = $log->listAll();
		$this->view->list = $list;
		
	}
	
	
	public function checkAposExistAction(){
		set_time_limit(0);
		$pList = new Model_DbTable_Productsva();
		if($_POST){
			$file = $_FILES['file']['tmp_name'];
			if(empty ($file)){
				$this->view->errorFlag2 = true;
					
				$this->view->errorMessage = "Please import the file";
				$this->renderScript('/products/importCsv.phtml');
				return false;
			}
		
			$arrResult = array();
		
			$fl = fopen($file,"r");
			while(($lineData = fgetcsv($fl,0)) != false){
				if(!empty($lineData[0])){
					$arrResult[$lineData[0]] = $lineData[1];
				}
			}
	
			fclose($fl);
			
			
			/*
			$expfolder =  getcwd()."/tmp/shop/";
			$file2 = "exist.txt";
			$file3 = "notexist.txt";
			$f2 = fopen($expfolder.$file2,"w");
			$f3 = fopen($expfolder.$file3,"w");
		
				$exitArr = array();
				$noexitArr = array();
				foreach ($arrResult as $k => $v){

						$barcode = $proDes->matchBcode(trim($k));
						
						if(empty($barcode)){
							//echo "Not Exist ".$v."  Null"."<br/>";
							//$noexitArr[] = $v;
							fputs($f3,$k."\t".$v."\t"."\n");
						}else{
							//echo "Exist ".$v."  Des: ".$barcode['DESCRIPT']."<br/>";
							//$exitArr[] = $v;
							fputs($f2,$k."\t".$barcode['DESCRIPT']."\t"."\n");
						}
						
				}
	
				fclose($f2);
				fclose($f3);
			*/
				
		}
		
	}
	/**
	 * Main control panel of WH,Accounts,Admin,It
	 */
	public function controlPannelAction(){
		
	}
	
	/**
	 * This function is for record all test and complete links
	 * Show/Edit status of test process link, assign complete link to department 
	 */
	public function itPannelAction(){
		$menu = new Model_DbTable_Updatelog();
		$date = Model_DatetimeHelper::dateToday();
		if(isset($_POST['submit'])){
			foreach ($_POST['id_link'] as $k => $v){
				if(!empty($_POST['status'][$k]) || !empty($_POST['fb'][$k])){
					$log = $menu->getLog($k);
					if($log['status'] != $_POST['status'][$k] || $log['feedback']!=$_POST['fb'][$k]){
						$menu->updateResult($k,$_POST['status'][$k],$_POST['fb'][$k],$date);
					}
				}
			}
		}
		if(isset($_POST['Assign'])){
			foreach ($_POST['fin_link'] as $k2 => $v2){
				if(!empty($_POST['depart'][$k2])){
					$menu->updateAssign($k2,$_POST['depart'][$k2]);
				}
			}
		}
		
		$menuList = $menu->listAllUnfinish();
		$fnmenuList = $menu->listAllfinish();
		
		
		
		$this->view->unfinishLink = $menuList;
		$this->view->finishLink = $fnmenuList;
		
		
	}
	
	/**
	 * This function is record password for Ken
	 */
	public function superPasswordAction(){
		
	}
	
	/**
	 * This function is for transfer php document into database
	 * 
	 */
	
	public function phpReadAction(){
		
		if($_POST){
			$file = $_FILES['file']['tmp_name'];
			$handle = fopen($file, "rb");
			$contents = "";
			while (!feof($handle)) {
				$contents .= fread($handle, filesize($file));
			}
			d($contents);
			$array = split("\n", $contents);
			$result = array();
			$class[] = array();
			array_filter($array);
			
			foreach ($array as $k => $cont){
				if(strchr($cont,'<h2 class="class-name">')){
					if(!empty($cont)){
						$class[] = strip_tags($cont);
					}
				}
				if(strchr($cont,'<span class="method-title">')){
					if(!empty($cont)){
						$result[$cont]['class_name']  = strip_tags($cont);
						$result[$cont]['description']  = strip_tags($array[$k+4]);
						$result[$cont]['explain']  = strip_tags($array[$k+5]);
					}
				}
			}
			d($array);
			d($class);
			d($result);
			fclose($handle);
		}

	}
	
	public function onceAddAction(){
		/*
		$pLocation = new Model_Products_Location();
		
		$xList = $pLocation->getListByLocation('X');
	
		
		foreach ($xList as $k => $v){
			$newLocation = str_replace('X', 'Y', $v['code_location']);
			echo $newLocation."<br/>";
			$pLocation->updateJustLocation($v['id_location'], $newLocation);
		}*/
		/*
		$f1 = fopen(getcwd()."/tmp/CBOrder.csv", "r");
		
		$exportFolder = getcwd()."/tmp/";
		$fullFileName = "orderCb.txt";
		$f2 = fopen($exportFolder.$fullFileName,"w");
		while(($lineData = fgetcsv($f1))!= FALSE){
			$productCode = trim($lineData[0]);
			$pDes = $lineData[1];
			$pOrderQty = $lineData[2];
			$urlStock = "http://".SERVER_IP_COMM."/products/get-current-stock/barcode/".$productCode;
			$stockWarehouse = Model_System_Downloadhelper::downloadUrl($urlStock);
			$location = $pLocation->getLocationsByProduct($productCode);
			$strFileHead = array($productCode,$pDes,$pOrderQty,$stockWarehouse);
			
			if($location){
				foreach ($location as $k => $v){
					array_push($strFileHead, $v['code_location']);
				}
			}
			$strTmp = implode("\t",$strFileHead);
			fputs($f2,$strTmp."\r\n");
		}
		fclose($f2);
		echo "Download File <a href=\"/tmp/orderCb.txt\" target=\"_blank\">HERE</a>";
		*/
		
		$ts = new Model_DbTable_Roster_Timesheet();
		$shop = unserialize(ARR_SHOP_ACTIVE);
		
		$rs = array();
		foreach ($shop as $v){
			$w19 = $ts->listConfirmedShiftByDateByShop($v, '2015-07-19', '2015-07-19');
			$w12 = $ts->listConfirmedShiftByDateByShop($v, '2015-07-12', '2015-07-12');
			$w05 = $ts->listConfirmedShiftByDateByShop($v, '2015-07-05', '2015-07-05');
			$w28 = $ts->listConfirmedShiftByDateByShop($v, '2015-06-28', '2015-06-28');
			
			foreach ($w19 as $k2 => $v2){
				$rs[$v][$v2['id_staff']]['2015-07-19'] = $v2; 
			}
			
			foreach ($w12 as $k2 => $v2){
				$rs[$v][$v2['id_staff']]['2015-07-12'] = $v2;
			}
			
			foreach ($w05 as $k2 => $v2){
				$rs[$v][$v2['id_staff']]['2015-07-05'] = $v2;
			}
			
			foreach ($w28 as $k2 => $v2){
				$rs[$v][$v2['id_staff']]['2015-06-28'] = $v2;
			}

		}

		$this->view->rs = $rs;

	}
	
	public function onceDisAction(){
		$dateBegin = $this->_getParam('date_begin');
		$dateEnd = $this->_getParam('date_end');
		
		
		$group = unserialize(ARR_SHOP_GROUP_MAPPING_RCENTER);
		$gList = unserialize(ARR_GROUP_SHOPS);
		
		
		$diff = Model_DatetimeHelper::dateDiff($dateBegin, $dateEnd);
		$rPart = new Model_DbTable_Pr_Prproducts(Zend_Registry::get('db_remoteapp'));
		$rMove = new Model_DbTable_Pr_Prstockmovement(Zend_Registry::get('db_remoteapp'));
		$invHead = substr($dateBegin,2,2);
		$invHead = "L".$invHead;
		
		$repairpartsDetail = $rMove->listByJobCodeId(2,$invHead,$dateBegin,$dateEnd);
		$rList = array();
		foreach ($repairpartsDetail as $k => $v){
			$amt = 0;
			if($group[$v['shop_code']] && $v['shop_code'] != 23){
			if($v['date_move'] < '2015-04-26'){
				
				$prDetail = $rPart->getProduct($v['id_product']);
				
				$rList[$group[$v['shop_code']][0]][$v['id_move']]  = $v;
				$rList[$group[$v['shop_code']][0]][$v['id_move']]['pr_code'] = $prDetail['code_product'];
				$rList[$group[$v['shop_code']][0]][$v['id_move']]['pr_des'] = $prDetail['title_product'];
				$rList[$group[$v['shop_code']][0]][$v['id_move']]['pr_price'] = 0;
				foreach ($gList[$group[$v['shop_code']][0]] as $shop => $shopN){
					$invoProducts = Model_Aposinit::initAposInvPro($shopN);
					$amt = $invoProducts->getRepairInvoiceTotal($v['id_job'], 0);
					if($amt){
						$rList[$group[$v['shop_code']][0]][$v['id_move']]['pr_price'] = $amt;
					}
				}

				/*
			if($v['shop_code'] == 4 ){
				$prDetail = $rPart->getProduct($v['id_product']);
				$invoProducts = Model_Aposinit::initAposInvPro('BHPC');
				$amt = $invoProducts->getRepairInvoiceTotal($v['id_job'], 0);
				$rList['BHPC'][$v['id_move']]  = $v;
				$rList['BHPC'][$v['id_move']]['pr_code'] = $prDetail['code_product'];
				$rList['BHPC'][$v['id_move']]['pr_des'] = $prDetail['title_product'];
				if($amt){
					$rList['BHPC'][$v['id_move']]['pr_price'] = $amt;
				}else{
					$rList['BHPC'][$v['id_move']]['pr_price'] = 0;
				}
			}else if ($v['shop_code'] == 10 ){
				$prDetail = $rPart->getProduct($v['id_product']);
				$invoProducts = Model_Aposinit::initAposInvPro('NLPC');
				$amt = $invoProducts->getRepairInvoiceTotal($v['id_job'], 0);
				$rList['NLPC'][$v['id_move']]  = $v;
				$rList['NLPC'][$v['id_move']]['pr_code'] = $prDetail['code_product'];
				$rList['NLPC'][$v['id_move']]['pr_des'] = $prDetail['title_product'];
				if($amt){
					$rList['NLPC'][$v['id_move']]['pr_price'] = $amt;
				}else{
					$rList['NLPC'][$v['id_move']]['pr_price'] = 0;
				}
			}else if ($v['shop_code'] == 12 ){
				$prDetail = $rPart->getProduct($v['id_product']);
				$invoProducts = Model_Aposinit::initAposInvPro('KCPC');
				$amt = $invoProducts->getRepairInvoiceTotal($v['id_job'], 0);
				$rList['KCPC'][$v['id_move']]  = $v;
				$rList['KCPC'][$v['id_move']]['pr_code'] = $prDetail['code_product'];
				$rList['KCPC'][$v['id_move']]['pr_des'] = $prDetail['title_product'];
				if($amt){
					$rList['KCPC'][$v['id_move']]['pr_price'] = $amt;
				}else{
					$rList['KCPC'][$v['id_move']]['pr_price'] = 0;
				}
			}else if($v['shop_code'] == 8 ){
				$prDetail = $rPart->getProduct($v['id_product']);
				$invoProducts = Model_Aposinit::initAposInvPro('EPIC');
				$amt = $invoProducts->getRepairInvoiceTotal($v['id_job'], 0);
				$rList['EPIC'][$v['id_move']]  = $v;
				$rList['EPIC'][$v['id_move']]['pr_code'] = $prDetail['code_product'];
				$rList['EPIC'][$v['id_move']]['pr_des'] = $prDetail['title_product'];
				if($amt){
					$rList['EPIC'][$v['id_move']]['pr_price'] = $amt;
				}else{
					$rList['EPIC'][$v['id_move']]['pr_price'] = 0;
				}
			}
			*/
			}else if($v['date_move'] >= '2015-04-26'){
				$prDetail = $rPart->getProduct($v['id_product']);
				
				$rList[$group[$v['shop_code']][0]][$v['id_move']]  = $v;
				$rList[$group[$v['shop_code']][0]][$v['id_move']]['pr_code'] = $prDetail['code_product'];
				$rList[$group[$v['shop_code']][0]][$v['id_move']]['pr_des'] = $prDetail['title_product'];
				$rList[$group[$v['shop_code']][0]][$v['id_move']]['pr_price'] = 0;
				foreach ($gList[$group[$v['shop_code']][0]] as $shop => $shopN){
					$invoProducts = Model_Aposinitnew::initAposInvPro($shopN);
					$amt = $invoProducts->getRepairInvoiceTotal($v['id_job'], 0);
					if($amt){
						$rList[$group[$v['shop_code']][0]][$v['id_move']]['pr_price'] = $amt;
					}
				}

				/*
				if($v['shop_code'] == 4 ){
					$prDetail = $rPart->getProduct($v['id_product']);
					$invoProducts = Model_Aposinitnew::initAposInvPro('BHPC');
					$amt = $invoProducts->getRepairInvoiceTotal($v['id_job'], 0);
					$rList['BHPC'][$v['id_move']]  = $v;
					$rList['BHPC'][$v['id_move']]['pr_code'] = $prDetail['code_product'];
					$rList['BHPC'][$v['id_move']]['pr_des'] = $prDetail['title_product'];
					if($amt){
						$rList['BHPC'][$v['id_move']]['pr_price'] = $amt;
					}else{
						$rList['BHPC'][$v['id_move']]['pr_price'] = 0;
					}
				}else if ($v['shop_code'] == 10 ){
					$prDetail = $rPart->getProduct($v['id_product']);
					$invoProducts = Model_Aposinitnew::initAposInvPro('NLPC');
					$amt = $invoProducts->getRepairInvoiceTotal($v['id_job'], 0);
					$rList['NLPC'][$v['id_move']]  = $v;
					$rList['NLPC'][$v['id_move']]['pr_code'] = $prDetail['code_product'];
					$rList['NLPC'][$v['id_move']]['pr_des'] = $prDetail['title_product'];
					if($amt){
						$rList['NLPC'][$v['id_move']]['pr_price'] = $amt;
					}else{
						$rList['NLPC'][$v['id_move']]['pr_price'] = 0;
					}
				}else if ($v['shop_code'] == 12 ){
					$prDetail = $rPart->getProduct($v['id_product']);
					$invoProducts = Model_Aposinitnew::initAposInvPro('KCPC');
					$amt = $invoProducts->getRepairInvoiceTotal($v['id_job'], 0);
					$rList['KCPC'][$v['id_move']]  = $v;
					$rList['KCPC'][$v['id_move']]['pr_code'] = $prDetail['code_product'];
					$rList['KCPC'][$v['id_move']]['pr_des'] = $prDetail['title_product'];
					if($amt){
						$rList['KCPC'][$v['id_move']]['pr_price'] = $amt;
					}else{
						$rList['KCPC'][$v['id_move']]['pr_price'] = 0;
					}
				}else if($v['shop_code'] == 8 ){
				$prDetail = $rPart->getProduct($v['id_product']);
				$invoProducts = Model_Aposinitnew::initAposInvPro('EPIC');
				$amt = $invoProducts->getRepairInvoiceTotal($v['id_job'], 0);
				$rList['EPIC'][$v['id_move']]  = $v;
				$rList['EPIC'][$v['id_move']]['pr_code'] = $prDetail['code_product'];
				$rList['EPIC'][$v['id_move']]['pr_des'] = $prDetail['title_product'];
				if($amt){
					$rList['EPIC'][$v['id_move']]['pr_price'] = $amt;
				}else{
					$rList['EPIC'][$v['id_move']]['pr_price'] = 0;
				}
				}*/
			}
			}
		}
		
		$this->view->list = $rList;
		/*
		$pLocation = new Model_Products_Location();
		$product = new Model_DbTable_Productsva();
	
		$f1 = fopen(getcwd()."/tmp/dis.csv", "r");

		while(($lineData = fgetcsv($f1))!= FALSE){
			$productCode = trim($lineData[0]);
			$product->discontinueProducts($productCode);
			echo $productCode."<br/>";
			
		}
		fclose($f1);
		*/
	}
	
	public function aposExportAction(){
		set_time_limit(0);

		$shopList = unserialize(ARR_SHOP_ACTIVE);
		$arrShopMaping = unserialize(ARR_APOS_NEW_SHOP_MAPPING);
		$excelFile = new Model_Fileshandler();
		$proDes = Model_Aposinitnew::initProDes();
		$pList = $proDes->getAll();
		//$pList = $proDes->listAllByDes('Screen');
		/*
		$f1 = fopen(getcwd()."/tmp/list.csv", "r");
		
		$pList = array();
		while(($lineData = fgetcsv($f1))!= FALSE){
	
			$pList[trim($lineData[0])]['SCODE'] = trim($lineData[0]);
			$pList[trim($lineData[0])]['UPRICE'] = trim($lineData[8]);
			
		}
		*/
		/*
		$list = array();
		$stock = new Model_DbTable_Apos_Stock_Balanceold(Zend_Registry::get('db_allapos'));
		foreach ($pList as $k => $v){
			$qty = $stock->getStockBalance($v['SCODE'],'WH');
			if(!empty($qty)){
				$list[trim($v['SCODE'])] = array($qty['BALANCE'],$v['UPRICE']);
			}else{
				$list[trim($v['SCODE'])] = array(0,$v['UPRICE']);
			}
			
		}
		$fileName = 'WH';
		$excelFile->exportAPOSList($list, $fileName);
		*/
		
		
		
		foreach ($shopList as $v2){
			if($v2 == 'CLPC'){
			$list = array();
			$stock = Model_Aposinitnew::initProStock($v2);
			
			
			// implement white black list
			foreach ($pList as $k => $v){
				$qty = $stock->getStockBalance($v['SCODE'],$arrShopMaping[$v2]);
				//$des = $proDes->getAposDes($v['SCODE']);
				if($qty['BALANCE'] > 0){
					$list[trim($v['SCODE'])] = array($qty['BALANCE'],$v["DESCRIPT"]);
				}
			}
			
			$excelFile->exportAPOSList($list, $v2);
			echo "<a href=\"http://".SERVER_IP_PUBLIC.":1180/export/APOS/{$v2}.xls\" >{$v2}.xls</a>";
			}
			
		}
		
		
	}
	
	
	public function sixNoSaleAction(){
		set_time_limit(0);

		$shopArr = unserialize(ARR_SHOP_ACTIVE);
		$pro = new Model_DbTable_Productsva();
		$pSales = new Model_DbTable_Products_Sales();

		$proList = $pro->listAllProductsNew();
		
		$product = "";
		
		$dateBegin = '2014-12-01';
		$dateEnd = '2015-01-30';
		
		$cot = 0;
		$arrPSales = array();
		$noSaleList = array();
		
		if($_POST){
			$file = $_FILES['file']['tmp_name'];
			if(empty ($file)){
				$this->view->errorFlag2 = true;
			
				$this->view->errorMessage = "Please import the file";
				$this->renderScript('/products/importCsv.phtml');
				return false;
			}
			
			$arrResult = array();
				
			$fl = fopen($file,"r");
				
			while(($lineData = fgetcsv($fl,0)) != false){
			
				$arrResult[] = $lineData[0];
			}
				
			fclose($fl);
			$list = array();
			foreach ($arrResult as $k => $v){
				$des = $pro->getProductByBarcode(trim($v));
				$qty = $pSales->getSalesByCode(trim($v));
				$list[$v]['des'] = $des['title_product'];
				$list[$v]['shop_qty'] = $qty['stock_shop'];
				$list[$v]['wh_qty'] = $qty['stock_wh'];
			}
			
			$file2 = "nosale.txt";
			$expfolder =  getcwd()."/timesheet_export/";
			
			$f2 = fopen($expfolder.$file2,"w");
			foreach ($list as $k => $v){
				
				fputs($f2,$k."\t".$v['des']."\t".$v['shop_qty']."\t".$v['wh_qty']."\r\n");
			}
			fclose($f2);
			
			
			
			/*
			foreach ($arrResult as $k => $v){
				foreach($shopArr as $shop){
					$invPro = Model_Aposinit::initAposInvPro($shop);
				
					$sales = $invPro->getProductSalesQtyByDates2($dateBegin ,$dateEnd,trim($v));
					if($sales == true){
					
					}else{
						$list[$sales] = $sales;
					}
				}
				
			}
			d($arrResult);
			d($list);
			foreach ($list as $k => $v){
				echo $k."<br/>";
			}*/
		}
		foreach($proList as $product ){
		
			if(!self::bCodeFilter($product['code_product'])){
				
				
				$actDateBegin = Model_DatetimeHelper::adjustWeeks("sub", $dateBegin, 180);
				$actDateEnd = $dateBegin;
		
				$totalSales = 0;
				$totalShopStock = 0;
				if($product['date_introduce'] < '2015-03-01'){
				$pResult = $pSales->getSalesByCode(trim($product['code_product']));
					if($pResult){
					
						$pTotal = $pResult['sales_w1'] + $pResult['sales_w2'] + $pResult['sales_w3'] + $pResult['sales_w4'] + $pResult['sales_w5'] +
						$pResult['sales_w6']+ $pResult['sales_w7'] + $pResult['sales_w8'];
							if($pTotal == 0){
								$arrPSales[] = $pResult['code_product'];
		
							}
					}
				}
			}
		}
		d($arrPSales);
		foreach ($arrPSales as $k => $v){
			echo $v."<br/>";
		}
			

				
		
	
	
	}
	
	private static function bCodeFilter($sCode){
	
		$sCode = trim($sCode);
		$sCode3 = substr($sCode,0,3);
		$sCode4 = substr($sCode,0,4);
		$sCode5 = substr($sCode,0,5);
		$sCode7 = substr($sCode,0,7);
	
		$res = false;
	
		if($sCode3 == "ONL" || $sCode4 == "STA-" || $sCode4 == "PC00" || $sCode5 == "STRAP" || $sCode5 == "CREAT" || $sCode5 == "SCI-0" || $sCode7 == "DELITEM"
				|| $sCode7 == "BATCNOK" || $sCode7 == "BATOMOT" || $sCode7 == "BATONOK" || $sCode7 == "BATOSE-" || $sCode7 == "PCSCREA" || $sCode7 == "VIPGIFT" ||
				$sCode == "CN" ||  $sCode == "INSTALL" || $sCode == "INSTALLATION" || $sCode == "FAULTY" || $sCode == "VOUCHER" || $sCode == "PHONEREPAIR" ||
				$sCode == "SERVICE" || $sCode == "PARTSSALES"){
				
			$res = true;
		}
	
		return $res;
	
	}
	
	public function cbOrderAction(){
		$pLocation = new Model_Products_Location();
		
		$f1 = fopen(getcwd()."/tmp/CBOrder.csv", "r");
		
		$exportFolder = getcwd()."/tmp/";
		$fullFileName = "orderCb.csv";
		$f2 = fopen($exportFolder.$fullFileName,"w");
		while(($lineData = fgetcsv($f1))!= FALSE){
			$productCode = trim($lineData[0]);
			$pDes = $lineData[1];
			$pOrderQty = $lineData[2];
			$urlStock = "http://".SERVER_IP_COMM."/products/get-current-stock/barcode/".$productCode;
			$stockWarehouse = Model_System_Downloadhelper::downloadUrl($urlStock);
			$location = $pLocation->getLocationsByProduct($productCode);
			$strFileHead = array($productCode,$pDes,$pOrderQty,$stockWarehouse);
			fputs($f2,$strFileHead);
			foreach ($location as $k => $v){
				fputs($f2,$v['code_location']);
			}
			
			fputs($f2,""."\r\n");
		}
		fclose($f2);
		echo "Download File <a href=\"/tmp/orderCb.csv\" target=\"_blank\">HERE</a>";
	}
	
	
	public function nlRepairAction(){
		
		$rJob = new Model_DbTable_Repairjob(Zend_Registry::get('db_remoteapp'));
		$pMove = new Model_DbTable_Pr_Prstockmovement(Zend_Registry::get('db_remoteapp'));
		$pInfo = new Model_DbTable_Pr_Prproducts(Zend_Registry::get('db_remoteapp'));
		$pList = array();
		//d($rList);
		$tmpjobList = $rJob->listAllFinishedJobsByDate('2015-04-29', '2015-05-31');
		foreach ($tmpjobList as $k => $v){
			if($v['repair_staff'] == 284){
				$jobList[] = $v['id_job'];
			}
		}
		
		foreach ($jobList as $v){
			if(!empty($v)){
				$tmp = $pMove->listByJobCodeId(2,$v);
					
				foreach ($tmp as $k2 => $v2){
					$pList[$v2['id_job']][$v2['id_product']] = $v2['qty_product'];
				}
			}
		}
		$costTotal = 0;
		$quto = 0;
		foreach ($pList as $jobId => $x){
			
			$price = $rJob->getQuotePrice($jobId);
			echo $jobId."\t".$price."<br/>";
			foreach ($x as $pId => $qty){
				$pDetail = $pInfo->getProduct($pId);
				$cost = $qty * $pDetail['price_t'];
				$costTotal += $cost;
				echo $pDetail['code_product']."\t".$pDetail['title_product']."\t".$cost."<br/>";
			}
			echo "<br/>";
			$quto += $price;
		}
		echo $costTotal."<br/>";
		echo $quto;
		
		
		/*
		$inv = Model_Aposinitnew::initAposInvPro('NLPC');
		$rList = $inv->getRepairInvByDate('2015-05-11', '2015-05-24');
		$rJob = new Model_DbTable_Repairjob(Zend_Registry::get('db_remoteapp'));
		$pMove = new Model_DbTable_Pr_Prstockmovement(Zend_Registry::get('db_remoteapp'));
		$pInfo = new Model_DbTable_Pr_Prproducts(Zend_Registry::get('db_remoteapp'));
		$pList = array();
		//d($rList);
		foreach($rList as $v){
			$jobList[] = $rJob->getJobByInvShop($v, 'NLPC');
		}
		foreach ($jobList as $v){
			if(!empty($v)){
			$tmp = $pMove->listByJobCodeId(2,$v);
			
			foreach ($tmp as $k2 => $v2){
				$pList[$v2['id_job']][$v2['id_product']] = $v2['qty_product'];
			}
			}
		}
		$costTotal = 0;
		foreach ($pList as $jobId => $x){
			echo $jobId."\t";
			foreach ($x as $pId => $qty){
				$pDetail = $pInfo->getProduct($pId);
				$cost = $qty * $pDetail['price_t'];
				$costTotal += $cost;
				echo $pDetail['code_product']."\t".$pDetail['title_product']."\t".$cost;
			}
			echo "<br/>";
		}
		echo $costTotal;
		*/
	}
}
?>